# 用面向对象,实现一个学生Python成绩管理系统;
# 学生的信息存储在文件中;学生信息的字段有(班级,学号,姓名, Python成绩)
# 实现对学生信息及成绩的增,删,改,查方法;

# class student_python ( object ):

#     def __init__( self, Class, ID, Name, Py_score ):
#         self.Class = Class
#         self.ID = ID
#         self.Name = Name
#         self.Py_score = Py_score

#     def add( self )


class Student( object ):

    def __init__( self, class_name, id, name, py_score ):
        self.class_name = class_name
        self.id = id
        self.name = name
        self.py_score = py_score

    def print_info( self ): # print the student's information
        print( 'Class Name: %s, ID: %s, Name: %s, Python Score: %s ' %( self.class_name, self.id, self.name, self.py_score ) )

    def get_info( self ): # return the student's information with a list
        info_list = []
        info_list.append( self.class_name )
        info_list.append( self.id )
        info_list.append( self.name )
        info_list.append( self.py_score )
        return info_list


class ManagementSystem( object ):

    def __init__( self ):
        self.students = []
    
    def add_info( self, student ):
        if self.search_info( student.get_info()[1] ):
            print( 'ID duplicate! Add failed!' )
        else:
            self.students.append( student )
            print( 'Added successfully!' )

    def delete_info( self, id ):
        if self.search_info( id ):
            for student in self.students:
                if id == student.get_info()[1]:
                    self.students.remove( student )
                    print( 'Deleted successfully!' )
        else:
            print( 'No such a student! Delete failed!' )

    def modify_info( self, student, new_student ):
        if self.search_info( student.get_info()[1] ):
            for each in self.students:
                if student.get_info()[1] == each.get_info()[1]:
                    self.students.remove( each )
                    self.students.append( new_student )
                    print( 'Modified successfully!' )
        else:
            print( 'No such a student! Modify failed!' )

    def search_info( self, id ):
        for student in self.students:
            if id == student.get_info()[1]:
                return self.constr_student( student.get_info() )

    # the three methods below are no need for the aesy test...
    def constr_student( self, infolist ): # construct a student object with a list
        return Student( infolist[0], infolist[1], infolist[2], infolist[3] )

    def get_all( self ): # return the students' information with a list
        all_students = []
        for stuent in self.students:
            all_students.append( stuent.get_info() )
        return all_students

    def get_stu_objects( self ): # get the objects of all the students
        return self.students


management = ManagementSystem()

student1 = Student( 'SW', 12, 'Jason', 98 )
student2 = Student( 'CS', 10, 'Austin', 90 )
student3 = Student( 'IS', 26, 'Mark', 88 )

management.add_info( student1 )
management.add_info( student2 )

print( 'after adding:' )
print( '>>>>All students: ', management.get_all() )
print( 'searching result: ', management.search_info( 12 ).get_info() )
management.delete_info( 100 )
management.delete_info( 12 )
print( 'after deleting:' )
print( '>>>>All students: ', management.get_all() )
management.modify_info( student1, student3 )
print( 'after modifing:' )
print( '>>>>All students: ', management.get_all() )
management.modify_info( student2, student3 )
print( 'after modifing:' )
print( '>>>>All students: ', management.get_all() )

print( '>' * 100 )
print( '>' * 100 )
print( '>' * 100 )
print()
print()



# for example...
prompt_info = '''
----------input your choice----------
1. add a student;
2. search a student;
3. modify a student's information;
4. delete a student
5. print all the student's information
-1. exit
-------------------------------------
'''

print( '----------Welcome to use this system----------' )
while True:
    print( prompt_info )
    choice = int( input( '>>>>input your choice: ' ) )
    if choice == -1: # exit
        print( 'Goodbye~' )
        break

    elif choice == 1: # add
        
        class_name = input( '>>>>input the class name: ' )
        id = int( input( '>>>>input the student\'s id: ' ) )
        name = input( '>>>>input the student\'s name: ' )
        py_score = int( input( '>>>>input the python score: ' ) )
        student = Student( class_name, id, name, py_score )
        management.add_info( student )

    elif choice == 2: # search
        
        id = int( input( '>>>>input the id of the student you want to search: ' ) )
        if management.search_info( id ):
            print( 'Student found! Result is: ', end = '' )
            management.search_info( id ).print_info()
        else:
            print( 'No such a student! Search failed!' )

    elif choice == 3: # modify

        all_students = management.get_stu_objects()
        print( '>>>>Below is all the information:' )
        for student in all_students:
            student.print_info()

        id = int( input( '>>>>input the id of the student that you want to modify: ' ) )
        if management.search_info( id ):
            target_stu = management.constr_student( management.search_info( id ).get_info() )
            class_name = input( '>>>>input the class name: ' )
            id = int( input( '>>>>input the student\'s id: ' ) )
            name = input( '>>>>input the student\'s name: ' )
            py_score = int( input( '>>>>input the python score: ' ) )
            student = Student( class_name, id, name, py_score )
            management.modify_info( target_stu, student )
        else:
            print( 'No such a student! Modify failed!' )
        

    elif choice == 4: # delete
        
        all_students = management.get_stu_objects()
        if all_students:
            print( '>>>>Below is all the information:' )
            for student in all_students:
                student.print_info()

            id = int( input( '>>>>input the id of the student that you want to delete: ' ) )
            management.delete_info( id )
        else:
            print( 'Not a record here! Can not delete anyone!' )

    elif choice == 5: # print all
        all_students = management.get_stu_objects()
        if all_students:
            print( '>>>>Below is all the information:' )
            for student in all_students:
                student.print_info()
        else:
            print( 'Not a record here!' )